package cn.kasa.basic.service.impl;

import cn.kasa.basic.mapper.BaseMapper;
import cn.kasa.basic.query.BasicQuery;
import cn.kasa.basic.service.IBaseService;
import cn.kasa.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public Integer queryCount(BasicQuery basicQuery) {
        return baseMapper.queryCount(basicQuery);
    }

    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void batchRemove(Long[] ids) {
        baseMapper.batchRemove(ids);
    }


    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    @Override
    public PageList<T> queryData(BasicQuery basicQuery) {

        Integer total = baseMapper.queryCount(basicQuery);

        if (total == 0){
            return new PageList<T>();
        }

        List<T> rows = baseMapper.queryData(basicQuery);

        return new PageList<T>(total, rows);
    }




}

